<template>
<div class="market_smart_coupon">
    <div class="search-box">
      <Row type="flex">
        <Row type="flex" justify="start" class="search-item-left">
          <Col class="search-item"><span class="search-label">活动名称：</span><Input placeholder="请输入" :maxlength="20"  v-model.trim="keywords"></Input></Col>
          <Col class="search-item">
            <span class="search-label">状态：</span>
            <Select v-model="status"
              class="select-shop-item"
              @on-change="search"
              style="width:180px">
              <Option v-for="item in allStatus" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          </Col>
          <Col class="search-item">
          <Button class="search-btn"
                  type='primary'
                  @click="search"
                  >查询</Button>
          </Col>
          <Col class="search-item">
          <Button class="search-btn"
                @click="reset">重置</Button>
          </Col>
        </Row>
        <Row type="flex" justify="end" class="search-item-right">
          <Col class="search-item"><Button class="search-btn" type="primary" @click="skipAdd" v-if="hasPermission('moms:market:smartcouponcreate')">新增</Button></Col>
        </Row>
      </Row>
    </div>
    <Table stripe class="table-list" :columns="columns" :data="data">
      <template slot-scope="{ row }" slot="activityStatus">
        <span>{{ getEnumsName(row.activityStatus, allStatus) }}</span>
      </template>
      <template slot-scope="{ row }" slot="action">
        <i class="iconsys icondetail" title="查看详情" @click="skipDetail(row)" v-if="hasPermission('moms:market:smartcoupondetail')"></i>
        <i class="iconsys iconedit" title="编辑" @click="skipEdit(row)" v-if="row.activityStatus === 0 && hasPermission('moms:market:smartcouponupdate')"></i>
        <Poptip confirm title="你确定要删除吗？" @on-ok="sureDel(row)"
        v-if="row.activityStatus === 0 && hasPermission('moms:market:smartcoupondelete')"
        style="text-align:left">
          <i class="iconsys icondel" title="删除"></i>
        </Poptip>
      </template>
    </Table>
    <Row type='flex' justify="end" style='padding: 20px'>
      <Col>
        <Page :total="total"
              :page-size='pageSize'
              :current='pageIndex'
              show-total
              show-sizer
              @on-change='changePageIndex'
              @on-page-size-change='changePageSize' />
      </Col>
    </Row>
  </div>
</template>

<script>
import enums from './enums'
import { mapState } from 'vuex'
import { getActiveLists, delActive } from '@/api/market/active.js'
export default {
  name: 'market_smart_coupon_list',
  mixins: [enums],
  data () {
    return {
      keywords: '',
      status: -1,
      total: 0,
      pageSize: 10,
      pageIndex: 1,
      loading: false,
      tableHeight: 0,
      data: [],
      columns: [
        { title: '活动名称', key: 'activityName' },
        { title: '会员筛选', key: 'limitTimes' },
        { title: '状态', slot: 'activityStatus' },
        { title: '发放时间', key: 'activityStart' },
        { title: '创建人', key: 'creatorName' },
        { title: '创建时间', key: 'createTime' }
      ]
    }
  },
  computed: {
    ...mapState({
      merchantId: state => state.user.merchantId
    })
  },
  watch: {
    '$route' (newRoute) {
      if (newRoute.name === 'market_smart_coupon_list') {
        this.getData()
      }
    }
  },
  mounted () {
    this.getData()
    if (this.hasPermission(['moms:market:smartcouponupdate', 'moms:market:smartcoupondelete', 'moms:market:smartcoupondetail'])) {
      this.columns.push({
        title: '操作',
        slot: 'action',
        width: 200
      })
    }
  },
  methods: {
    skipAdd () {
      this.$router.push({ name: 'market_smart_coupon_create' })
    },
    skipDetail (row) {
      this.$router.push({
        name: 'market_smart_coupon_detail',
        params: {
          id: row.activityId
        }
      })
    },
    skipEdit (row) {
      this.$router.push({
        name: 'market_smart_coupon_edit',
        params: {
          id: row.activityId
        }
      })
    },
    getData () {
      this.loading = true
      this.tableHeight = 0
      let params = {
        merchantId: this.merchantId,
        activityName: this.keywords,
        activityType: 9,
        offset: (this.pageIndex - 1) * this.pageSize,
        limit: this.pageSize
      }
      if (this.status !== -1) {
        params.activityStatus = this.status
      }
      getActiveLists(params).then(res => {
        this.loading = false
        const result = res.data || {}
        if (result.status === 200) {
          this.data = result.data || []
          this.total = result.totalCount || 0
          if (this.data.length > 10) {
            this.tableHeight = 520
          }
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
        this.loading = false
      })
    },
    sureDel (row) {
      delActive({
        activityId: row.activityId
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.$Message.success('删除成功！')
          setTimeout(() => {
            this.getData()
          }, 300)
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
      })
    },
    search () {
      this.pageIndex = 1
      this.getData()
    },
    reset () {
      this.keywords = ''
      this.status = -1
      this.search()
    },
    changePageIndex (page) {
      this.pageIndex = page
      this.getData()
    },
    changePageSize (size) {
      this.pageSize = size
      this.search()
    }
  }
}
</script>
